package com.lsfb.signsys.main;

import android.app.Activity;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.lsfb.signsys.BaseActivity;
import com.lsfb.signsys.R;
import com.lsfb.signsys.my.MyFragment;
import com.lsfb.signsys.my.MyInfoBean;
import com.lsfb.signsys.position.PositionFragment;
import com.lsfb.signsys.utils.Utils;
import com.lsfb.signsys.view.BottomNavView.BottomNavView;
import com.lsfb.signsys.view.BottomNavView.IBadge;
import com.lsfb.signsys.view.simpletoolbar.SimpleToolbar;

import butterknife.BindView;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by zgscwjm on 2018/3/23.
 */

public class MainActivtiy extends BaseActivity {

    @BindView(R.id.act_main_navigation)
    BottomNavView bottomNavView;

    private PositionFragment mPositionFragment;

    private MyFragment mMyFragment;
    private Fragment mFragment;


    @BindView(R.id.act_main_toolbar)
    SimpleToolbar mSimpleToolbar;

    @BindView(R.id.toolbar)
    Toolbar mToolbar;



    @Override
    public int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    public void init() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        MyInfoBean myInfoBean = new MyInfoBean();
        myInfoBean.setJobsName("范德萨范德萨");
        myInfoBean.setJobs(1);
        myInfoBean.setUserName("我好困");
        Utils.getInstall().setInfoBean(myInfoBean);



        IBadge iBadge = bottomNavView.getBadge(1);
        iBadge.BadgeVisibility(true);
        iBadge.setBadgeMole(1);
        iBadge.setCounter(4);


        IBadge iBadge2 = bottomNavView.getBadge(2);

        iBadge2.setBadgeMole(0);
        iBadge2.BadgeVisibility(true);


        mPositionFragment = new PositionFragment();

        mMyFragment = new MyFragment();



        addFragmentToActivity(getSupportFragmentManager(), mPositionFragment, R.id.act_main_content);
        addFragmentToActivity(getSupportFragmentManager(), mMyFragment, R.id.act_main_content);
        bottomNavView.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.navigation_postion:
                    mSimpleToolbar.setVisibility(View.VISIBLE);
                    mSimpleToolbar.setTitle("定位");
                    switchContent(mPositionFragment);
                    return true;
                case R.id.navigation_leave:
                    mSimpleToolbar.setVisibility(View.VISIBLE);
                    mSimpleToolbar.setTitle("请假");
                    return true;
                case R.id.navigation_statistics:
                    mSimpleToolbar.setVisibility(View.VISIBLE);
                    mSimpleToolbar.setTitle("统计");
                    Log.e("lsfb", "size" + getSupportFragmentManager().getFragments().size());
                    return true;
                case R.id.navigation_me:
                    mSimpleToolbar.setVisibility(View.GONE);
                    switchContent(mMyFragment);
                    return true;
            }
            return false;
        });

    }

    private void switchContent(Fragment mFragment) {
        getSupportFragmentManager().beginTransaction().hide(getSupportFragmentManager().findFragmentById(R.id.act_main_content)).
                show(mFragment).commit();
    }


    public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager,
                                             @NonNull Fragment fragment, int frameId) {
        checkNotNull(fragmentManager);
        checkNotNull(fragment);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(frameId, fragment);
        transaction.commit();
    }


}
